export default defineContentScript({
  matches: ['*://*/*'],
  main() {
    // 监听来自扩展的消息
    browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
      const currentUrl = window.location.href;
      if (message.action === 'paste') {
        handlePasteMessage(message.content, currentUrl, sendResponse);
        return true;
      }
      return true;
    });

    // 处理粘贴消息
    function handlePasteMessage(content: string, url: string, sendResponse: (response?: any) => void) {
      try {
        if (url.includes('gemini.google.com')) {
          // Gemini 特殊处理
          const editorDiv = document.querySelector('div.ql-editor');
          if (editorDiv) {
            editorDiv.innerHTML = '<p>' + content + '</p>';
            sendResponse({ status: 'success', message: '内容已成功发送' });
          }
          sendResponse({ status: 'error', message: '未找到Gemini编辑区域' });
        } else {
          sendResponse({ status: 'error', message: '暂不支持此网站发送' });
        }
      } catch (error) {
        console.error('粘贴内容时出错:', error);
        sendResponse({ status: 'error', message: '粘贴失败' });
      }
    }
  },
});
